package com.mercadoutils.listeners;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class SesionListener implements HttpSessionListener {
		
	private static int totalActiveSessions;
	 
    public static int getTotalActiveSession(){
          return totalActiveSessions;
    }
    
	@Override
    public void sessionCreated(HttpSessionEvent arg0) {
           totalActiveSessions++;
           System.out.println("sessionCreated - add one session into counter");	
    }
 
	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
	       totalActiveSessions--;
	       System.out.println("sessionDestroyed - deduct one session from counter");	
	}	
	
	public static HttpSession getSession() {
	    ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
	    return attr.getRequest().getSession(true);
	}
	
}
